﻿using System.Collections.Generic;
using System.Linq;
using StackExchange.Redis;
using studentSubsidy.Dao;
using studentSubsidy.Entity;
using studentSubsidy.Entity.AdminSigned;
using studentSubsidy.Entity.Apply;
using studentSubsidy.Models;

namespace studentSubsidy.Service
{
    public class AdminSignedService
    {
        private AdminSignedDao _adminSignedDao = new AdminSignedDao();
        private ApplyDao _applyDao = new ApplyDao();

        public Result getSignedList(string role)
        {
            List<Apply> applies = _adminSignedDao.getSignedList(role);
            List<AllApplyReturn> signedListReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                if (project.isRefund == 1 && applies[i].isSigned == 0)
                {
                    var applyReturn = new AllApplyReturn
                    {
                        applyId = applies[i].apply_id,
                        applyTime = applies[i].apply_time.Replace("T"," "),
                        projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                        address = user.address,
                        name = user.name,
                        major = user.major,
                        phone = user.phone
                    };
                    signedListReturn.Add(applyReturn);
                }
            }

            return new Result(200, "搜索成功", signedListReturn, null);
        }

        public Result getHasSignedList(string role)
        {
            List<Apply> applies = _adminSignedDao.getHasSignedList(role);
            List<AllApplyReturn> signedListReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                if (project.isRefund == 1 && applies[i].isSigned == 1)
                {
                    var applyReturn = new AllApplyReturn
                    {
                        applyId = applies[i].apply_id,
                        applyTime = applies[i].apply_time.Replace("T"," "),
                        projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                        address = user.address,
                        name = user.name,
                        major = user.major,
                        phone = user.phone
                    };
                    signedListReturn.Add(applyReturn);
                }
            }

            return new Result(200, "搜索成功", signedListReturn, null);
        }

        public Result getSignedInfo(int applyId)
        {
            List<Signed_File> signedFiles = _adminSignedDao.getSignedFileById(applyId);
            SignedInfoReturn signedInfoReturn = new SignedInfoReturn
            {
                signed_first = "null",
                signed_second = "null",
                filePath = "null",
            };
            if (signedFiles.Count == 0)
            {
                return new Result(400, "数据为空", null, null);
            }

            foreach (var VARIABLE in signedFiles)
            {
                if (VARIABLE.fileType == 0)
                {
                    signedInfoReturn.filePath = VARIABLE.filePath;
                }

                if (VARIABLE.fileType == 1)
                {
                    signedInfoReturn.signed_first = VARIABLE.filePath;
                }

                if (VARIABLE.fileType == 2)
                {
                    signedInfoReturn.signed_second = VARIABLE.filePath;
                }
            }

            return new Result(200, "获取成功", signedInfoReturn, null);
        }

        public Result deleteSignedFile(int applyId, int type)
        {
            _adminSignedDao.deleteSignedFile(applyId, type);
            return new Result(200, "删除成功", "", "");
        }

        public Result rejectSigned(int applyId)
        {
            _adminSignedDao.rejectSigned(applyId);
            return new Result(200, "驳回成功", "", "");
        }

        public Result getIcon(int applyId)
        {
            Signed_File signedFile = _adminSignedDao.getUserSignedFile(applyId);
            if (signedFile.isCheck==-1)
            {
                return new Result(200, "", "error","");
            }else
                return new Result(200, "", "success","");
        }

        public Result signedSuccess(int applyId)
        {
            _adminSignedDao.signedFileSuccess(applyId);
            return new Result(200, "", "", "");
        }

        public Result isSuccess(int applyId)
        {
            Apply apply = _applyDao.getApplyByApplyId(applyId);
            if (apply.isSigned == 1)
            {
                return new Result(200, "", "", "");
            }
            return new Result(400, "", "", "");
        }

        public Result getSignedNeedCheckList(string role)
        {
            List<Apply> applies = _adminSignedDao.getSignedNeedCheckList(role);
            List<AllApplyReturn> signedListReturn = new List<AllApplyReturn>();

            for (int i = 0; i < applies.Count; i++)
            {
                Project project = _applyDao.getProjectById(applies[i].project_id);
                User user = _applyDao.getUser(applies[i].user_id);
                if (project.isRefund == 1 && applies[i].isSigned != 0)
                {
                    var applyReturn = new AllApplyReturn
                    {
                        applyId = applies[i].apply_id,
                        applyTime = applies[i].apply_time.Replace("T"," "),
                        projectName = _applyDao.getProjectById(applies[i].project_id).product_name,
                        address = user.address,
                        name = user.name,
                        major = user.major,
                        phone = user.phone
                    };
                    signedListReturn.Add(applyReturn);
                }
            }

            return new Result(200, "", signedListReturn, null);
        }
    }
}